import router from "./index";
import userIndex from "./modulesPage/userIndex";
import adminIndex from "./modulesPage/adminIndex"

const dynamically = ''

function FileterRoutes(dynamically, roles, isInit) {
    let result = [];
    if (roles.length === 1 && isInit) {
        dynamically = [...userIndex]
        dynamically.forEach(v => v.roles = roles)
    } else if (isInit) {
        dynamically = [...adminIndex]
    }
    dynamically.forEach(route => {
        if (route.roles.some(role => roles.includes(role))) {
            // 判断是否有子路由
            if (route.children && Array.isArray(route.children) && route.children.length > 0) {
                route.children = FileterRoutes(route.children, roles, false)
            }
            result.push(route)
        }
    })
    return result
}

// 动态添加路由配置
function addRoutes(routes, parent) {
    // route 路由配配置对象
    // parent 要追加路由的父组件的名称
    routes.forEach(route => {
        if (parent) {
            router.addRoute(parent, route)
        } else {
            router.addRoute(route)
        }
        // 判断是否有子路由
        if (route.children && Array.isArray(route.children) && route.children.length > 0) {
            // 递归添加子路由
            addRoutes(route.children, route.name)
        }
    })
}

export default {
    FileterRoutes,
    dynamically,
    addRoutes
}

